home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Ian & Stuart's Australian Mac: Not for Sale
/
Another.not.for.sale (Australia).iso
/
fade into you
/
getting there
/
Apps
/
MOO-1.7.6.src
/
src
/
functions.c
< prev
next >
Wrap
Text File
|
1994-11-02
|
9KB
|
340 lines
/******************************************************************************
Copyright (c) 1992 Xerox Corporation. All rights reserved.
Portions of this code were written by Stephen White, aka ghond.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export
control laws. This software is made available AS IS, and Xerox Corporation
makes no warranty about the software, its performance or its conformity to
any specification. Any person obtaining a copy of this software is requested
to send their name and post office or electronic mail address to:
Pavel Curtis
Xerox PARC
3333 Coyote Hill Rd.
Palo Alto, CA 94304
Pavel@Xerox.Com
*****************************************************************************/
#include "my-stdarg.h"
#include "config.h"
#include "functions.h"
#include "log.h"
#include "storage.h"
#include "structures.h"
#include "utils.h"
/*****************************************************************************
* This is the table of procedures that register MOO built-in functions. To
* add new built-in functions to the server, add to the list below the name of
* a C function that will register your new MOO built-ins; your C function will
* be called exactly once, during server initialization. Also add a
* declaration of that C function to `bf_register.h' and add the necessary .c
* files to the `CSRCS' line in the Makefile.
****************************************************************************/
typedef void (*registry)();
static registry bi_function_registries[] = {
register_execute,
register_list,
register_log,
register_numbers,
register_objects,
register_property,
register_server,
register_tasks,
register_verbs
};
void
register_bi_functions()
{
int loop, num_registries =
sizeof(bi_function_registries) / sizeof(bi_function_registries[0]);
for (loop = 0; loop < num_registries; loop++)
(void) (*(bi_function_registries[loop]))();
}
/*** register ***/
static struct bft_entry bf_table[MAX_FUNC];
static unsigned top_bf_table = 0;
static unsigned
register_common(const char *name, int minargs, int maxargs, bf_type func,
bf_read_type read, bf_write_type write, va_list args)
{
int va_index;
int num_arg_types = maxargs == -1 ? minargs : maxargs;
if (top_bf_table == MAX_FUNC) {
errlog("too many functions. %s cannot be registered.\n", name);
return 0;
}
bf_table[top_bf_table].name = str_dup(name);
bf_table[top_bf_table].minargs = minargs;
bf_table[top_bf_table].maxargs = maxargs;
bf_table[top_bf_table].func = func;
bf_table[top_bf_table].read = read;
bf_table[top_bf_table].write = write;
if (num_arg_types > 0)
bf_table[top_bf_table].prototype =
mymalloc(num_arg_types * sizeof(var_type), M_VL_LIST);
else
bf_table[top_bf_table].prototype = 0;
for (va_index = 0; va_index < num_arg_types; va_index++)
bf_table[top_bf_table].prototype[va_index] = va_arg(args, var_type);
return top_bf_table++;
}
unsigned
register_function(const char *name, int minargs, int maxargs,
bf_type func, ...)
{
va_list args;
unsigned ans;
va_start(args, func);
ans = register_common(name, minargs, maxargs, func, 0, 0, args);
va_end(args);
return ans;
}
unsigned
register_function_with_read_write(const char *name, int minargs, int maxargs,
bf_type func, bf_read_type read,
bf_write_type write, ...)
{
va_list args;
unsigned ans;
va_start(args, write);
ans = register_common(name, minargs, maxargs, func, read, write, args);
va_end(args);
return ans;
}
void
free_bf_table(void)
{
/* what an idea !!! */
unsigned i;
for (i = 0; i < top_bf_table; i++) {
free_str(bf_table[i].name);
if (bf_table[i].prototype)
myfree((void *) bf_table[i].prototype, M_VL_LIST);
}
}
/*** looking up functions -- by name or num ***/
static const char *func_not_found_msg = "no such function";
const char *
name_func_by_num(unsigned n) /* used by unparse only */
{
if (n >= top_bf_table)
return func_not_found_msg;
else
return bf_table[n].name;
}
unsigned
number_func_by_name(const char *name) /* used by parser only */
{
unsigned i;
for (i = 0; i < top_bf_table; i++)
if (!mystrcasecmp(name, bf_table[i].name))
return i;
return FUNC_NOT_FOUND;
}
/*** calling built-in functions ***/
package
call_bi_func(unsigned n, Var arglist, Byte func_pc,
Objid progr, void *vdata)
/* requires arglist.type == TYPE_LIST
call_bi_func will free arglist */
{
struct bft_entry f;
if (n >= top_bf_table) {
errlog("CALL_BI_FUNC: Unknown function number: %d\n", n);
free_var(arglist);
return no_var_pack();
}
f = bf_table[n];
if (func_pc == 1) { /* check arg types and count *ONLY* for first entry */
int k, max;
Var *args = arglist.v.list;
/*
* Check argument count
* (Can't always check in the compiler, because of @)
*/
if (args[0].v.num < f.minargs
|| (f.maxargs != -1 && args[0].v.num > f.maxargs)) {
free_var(arglist);
return make_error_pack(E_ARGS);
}
/*
* Check argument types
*/
max = (f.maxargs == -1) ? f.minargs : args[0].v.num;
for (k = 0; k < max; k++)
if (f.prototype[k] != TYPE_ANY
&& f.prototype[k] != args[k + 1].type) {
free_var(arglist);
return make_error_pack(E_TYPE);
}
}
/*
* do the function
*/
return (*(f.func))(arglist, func_pc, vdata, progr);
/* f.func is responsible for freeing/using up arglist.
e.g., bf_pass on func_pc==2 will just use up (return) arglist.
well, tail recursion could be implemented in this special case */
}
void
write_bi_func_data(void *vdata, Byte f_id)
{
if (f_id >= top_bf_table)
errlog("WRITE_BI_FUNC_DATA: Unknown function number: %d\n", f_id);
else if (bf_table[f_id].write)
(*(bf_table[f_id].write))(vdata);
}
int
read_bi_func_data(FILE *f, Byte f_id, void **bi_func_state)
{
if (f_id >= top_bf_table) {
errlog("READ_BI_FUNC_DATA: Unknown function number: %d\n", f_id);
*bi_func_state = 0;
return -1;
} else if (bf_table[f_id].read) {
*bi_func_state = (*(bf_table[f_id].read))(f);
if (*bi_func_state == 0) {
errlog("READ_BI_FUNC_DATA: Can't read data for %s()\n",
bf_table[f_id].name);
return -1;
}
} else
*bi_func_state = 0;
return 0;
}
package
make_error_pack(enum error err)
{
package p;
p.why = BI_RAISE;
p.func_data = 0;
p.func_pc = 0;
p.u.err = err;
return p;
}
package
make_var_pack(Var v)
{
package p;
p.why = BI_RETURN;
p.func_data = 0;
p.func_pc = 0;
p.u.ret = v;
return p;
}
package
no_var_pack(void)
{
Var v;
v.type = TYPE_NUM;
v.v.num = 0;
return make_var_pack(v);
}
package
make_call_pack(Byte func_pc, void *func_data)
{
package p;
p.why = BI_CALL;
p.func_data = func_data;
p.func_pc = func_pc;
return p;
}
char rcsid_functions[] = "$Id: functions.c,v 1.14 1992/10/23 23:03:47 pavel Exp $";
/* $Log: functions.c,v $
* Revision 1.14 1992/10/23 23:03:47 pavel
* Added copyright notice.
*
* Revision 1.13 1992/10/23 19:25:30 pavel
* Eliminated all uses of the useless macro NULL..
*
* Revision 1.12 1992/10/21 03:02:35 pavel
* Converted to use new automatic configuration system.
*
* Revision 1.11 1992/10/17 20:31:03 pavel
* Changed return-type of read_bi_func_data() from char to int, for systems
* that use unsigned chars.
* Global rename of strdup->str_dup, strref->str_ref, vardup->var_dup, and
* varref->var_ref.
* Fixed bug in register_common() that sometimes read the wrong number of
* argument-types from the argument list.
*
* Revision 1.10 1992/09/08 22:02:46 pjames
* Updated register_* list to call functions by their new names.
*
* Revision 1.9 1992/08/28 16:01:31 pjames
* Changed myfree(*, M_STRING) to free_str(*).
*
* Revision 1.8 1992/08/14 01:20:25 pavel
* Made it entirely clear how to add new MOO built-in functions to the server.
*
* Revision 1.7 1992/08/14 00:40:31 pavel
* Added missing #include "my-stdarg.h"...
*
* Revision 1.6 1992/08/14 00:01:03 pavel
* Converted to a typedef of `var_type' = `enum var_type'.
*
* Revision 1.5 1992/08/13 21:27:11 pjames
* Added register_bi_functions() which calls all procedures which
* register built in functions. To add another such procedure, just add
* it to the static list and to functions.h
*
* Revision 1.4 1992/08/12 01:48:42 pjames
* Builtin functions may now have as many arguments as desired.
*
* Revision 1.3 1992/08/10 17:39:46 pjames
* Changed registration method to use var_args. Changed 'next' field to
* be func_pc.
*
* Revision 1.2 1992/07/21 00:02:29 pavel
* Added rcsid_<filename-root> declaration to hold the RCS ident. string.
*
* Revision 1.1 1992/07/20 23:23:12 pavel
* Initial RCS-controlled version.
*/